package com.qipay.mapper;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CpInfoSqlProvider extends com.qipay.baas.mapper.CpInfoSqlProvider{
    public String selectByCondition(Map<String, Object> params) {
        if (params != null && params.size() > 2) {
            List<String> lst = buildConditionSql(params);
            return "select * from (" + StringUtils.join(lst.toArray(), " union ") +
                    ") a ORDER BY cp_id desc LIMIT #{offset},#{size}";
        }
        return "SELECT * FROM `cp_info` ORDER BY cp_id desc LIMIT #{offset},#{size}";

    }

    public String countByCondition(Map<String, Object> params) {
        if (params != null && params.size() > 0) {
            List<String> lst = buildConditionSql(params);
            return "select COUNT(0) from(select cp_id from (" + StringUtils.join(lst.toArray(), " union ") +
                    ") a) b";
        }
        return "SELECT count(0) FROM `cp_info` LIMIT 1";

    }

    List<String> buildConditionSql(Map<String, Object> params) {
        List<String> lst = new ArrayList<>(4);
        if (params.getOrDefault("cp_id", null) != null) {
            lst.add("SELECT * FROM `cp_info` where `cp_id` =#{cpId}");
        }    if (params.getOrDefault("name", null) != null) {
            lst.add("SELECT * FROM `cp_info` where `name` like concat(#{name},'%')");
        }
        if (params.getOrDefault("legal_person", null) != null) {
            lst.add("SELECT * FROM `cp_info` where `legal_person` like concat(#{legal_person},'%')");
        }
        if (params.getOrDefault("contacts", null) != null) {
            lst.add("SELECT * FROM `cp_info` where `contacts` like concat(#{contacts},'%')");
        }
        if (params.getOrDefault("contacts_mobile", null) != null) {
            lst.add("SELECT * FROM `cp_info` where `contacts_mobile` like concat(#{contacts_mobile},'%')");
        }
        return lst;
    }

    public String selectByAgentId(Map<String, Object> params) {
        if (params != null && params.size() > 2) {
            List<String> lst = buildAgentIdSql(params);
            boolean hasAgentId = params.getOrDefault("agent_id", null) != null;
            if(lst.size()>0) {
                return "select * from (" + StringUtils.join(lst.toArray(), " union ") +
                        ") a ORDER BY cp_id desc LIMIT #{offset},#{size}";
            }
            return "SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where  c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":"") +" ORDER BY cp_id desc LIMIT #{offset},#{size}";
        }
        return "SELECT * FROM `cp_info` ORDER BY cp_id desc LIMIT #{offset},#{size}";

    }

    public String countByAgentId(Map<String, Object> params) {
        if (params != null && params.size() > 0) {
            boolean hasAgentId = params.getOrDefault("agent_id", null) != null;
            List<String> lst = buildAgentIdSql(params);
            if (lst.size() >0) {
                return "select COUNT(0) from(select cp_id from (" + StringUtils.join(lst.toArray(), " union ") +
                        ") a) b";
            }
            return "SELECT count(id) FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":"");
        }
        return "SELECT count(0) FROM `cp_info` LIMIT 1";
        //and c2.agent_id=#{agent_id}
    }

    List<String> buildAgentIdSql(Map<String, Object> params) {
        List<String> lst = new ArrayList<>(5);
        //and c2.agent_id=#{agent_id}
        boolean hasAgentId = params.getOrDefault("agent_id", null) != null;

        if (params.getOrDefault("cp_id", null) != null) {
            lst.add("SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c1.`cp_id` =#{cp_id} and c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":""));
        }
        if (params.getOrDefault("name", null) != null) {
            lst.add("SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c1.`name` like concat(#{name},'%') and c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":""));
        }
        if (params.getOrDefault("legal_person", null) != null) {
            lst.add("SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c1.`legal_person` like concat(#{legal_person},'%') and c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":""));
        }
        if (params.getOrDefault("contacts", null) != null) {
            lst.add("SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c1.`contacts` like concat(#{contacts},'%') and c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":""));
        }
        if (params.getOrDefault("contacts_mobile", null) != null) {
            lst.add("SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c1.`contacts_mobile` like concat(#{contacts_mobile},'%') and c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":""));
        }
        if (params.getOrDefault("account_state", null) != null) {
            lst.add("SELECT c1.* FROM `cp_info` c1 join cp c2 on c1.cp_id=c2.id where c2.`account_state`=#{account_state} and c2.`type`=#{type} "+(hasAgentId?"and c2.agent_id=#{agent_id}":""));
        }
        return lst;
    }
}